using QQZiFramework;
using System;
using System.Collections.Generic;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using QQZiFramework.Mvc;

namespace QQZiCore.Mvc
{
    public class ValidateCodeHelper
    {
        DBContext m_context;

        public static ValidateCodeHelper GetHelper(DBContext context, string name)
        {
            ValidateCodeHelper helper = context.GetSession<ValidateCodeHelper>("ValidateCodeHelper_" + name);
            if (helper == null)
            {
                helper = new ValidateCodeHelper(name);
            }
            helper.m_context = context;
            return helper;
        }
        public string Name { get; set; }
        public int SendNumber { get; set; }

        public VerificationSMSData SMSData { get; set; }

        public ValidateCodeHelper(string name)
        {
            Name = name;
        }


        //创建图形验证码
        public static byte[] CreateImgCode(DBContext context)
        {
            int len = context.GetSettingValue<int>("Basic:ImgCodeLeng", false, 5);
            string imgCode = Utility.CreateValidateCode(len);
            context.SetSession<string>("imgCode", imgCode);
            return Utility.CreateValidateGraphic(imgCode);
        }


        /// <summary>
        /// 注册和修改手机号码发送短信验证码
        /// </summary>
        /// <returns></returns>
        //[HttpPost]
        //[LoginCheckFilter(IsCheck = false)]
        public JsonMessage SentVerifyCode(string mobile, string validation)
        {

            JsonMessage jm = new JsonMessage();

            bool needVerifyCode = m_context.GetSettingValue<bool>("Basic:NeedVerifyCode", false, true);
            if (needVerifyCode)
            {
                string imgCode = m_context.GetSession<string>("imgCode");
                if (imgCode == null || validation == null)
                {
                    jm.Tip = "NeedCode";
                    jm.Message = "请输入图形验证码";
                    return jm;
                }
                //图形验证码匹配
                if (!imgCode.ToUpper().Equals(validation.ToUpper()))
                {
                    jm.Message = "图形验证码匹配错误!";
                    return jm;
                }
                //清除图形验证码Session
                m_context.RemoveSession("imgCode");
            }
            //发送短信验证码
            throw new Exception("未实现发短信的方法");
            //string code =  m_context.GetBusiness<VerificationDriver>().SentSMS(mobile);
            string code = null;
            this.SMSData = new VerificationSMSData(mobile, code);
            m_context.SetSession<ValidateCodeHelper>("ValidateCodeHelper_" + Name, this);
            this.SendNumber++;
            jm.Result = "已发送短信验证码!";
            return jm;
        }

        public bool VerifySMS(string mobile, string smsCode)
        {
            //手机验证码比对
            if (this.SMSData == null) throw new ClientException("请先发送验证码");
            m_context.LogMessage(mobile + "点击登陆，Session验证码：" + this.SMSData.Code + ",前端上传验证码：" + smsCode);
            bool ok = this.SMSData.Verify(mobile, smsCode);
            if (ok)
            {
                this.SMSData = null;
                m_context.RemoveSession("ValidateCodeHelper_" + Name);
            }
            else
            {
                throw new ClientException("短信验证码错误");
            }
            return ok;
        }

    }

    public class VerificationSMSData
    {

        public VerificationSMSData(string mobile, string code)
        {
            Mobile = mobile;
            Code = code;
        }

        public bool Verify(string mobile, string code)
        {
            if (mobile == Mobile && code == Code) return true;
            else return false;
        }
        public string Mobile { get; set; }
        public string Code { get; set; }
    }
}